package com.toutiao.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.action.CoreController;
import com.base.dao.model.Grid;
import com.base.dao.model.Ret;
import com.base.dialect.PaginationSupport;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.toutiao.dao.model.NewsComment;
import com.toutiao.dao.model.NewsCommentExample;
import com.toutiao.service.NewsCommentService;

@RequestMapping("newsComment")
@Controller
@LoginFilter
public class NewsCommentController extends CoreController{

	@Autowired
	private NewsCommentService newsCommentService;

	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows,String newsId){
		PaginationSupport.byPage(page, rows);
		NewsCommentExample example = new NewsCommentExample();
		example.createCriteria().andNewsIdEqualTo(Integer.valueOf(newsId));
		List<NewsComment> list = newsCommentService.selectByExample(example);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(NewsComment newsComment){
		if (newsComment.getId() == null){
			newsCommentService.insert(newsComment);
		}else{
			newsCommentService.updateByPrimaryKeySelective(newsComment);
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(Integer id){
		NewsComment newsComment = newsCommentService.selectByPrimaryKey(id);
		return JSONUtils.serialize(newsComment);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			newsCommentService.deleteByPrimaryKey(Integer.parseInt(str));
		}
		return JSONUtils.serialize(new Ret(0));
	}

}